package com.crecheng.shop.controller;

import com.alibaba.fastjson.JSON;
import com.crecheng.shop.DAO.AddressData;
import com.crecheng.shop.Manage.AddressManage;
import com.crecheng.shop.util.MyMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AddressController {

    @RequestMapping("/address/get")
    public @ResponseBody
    String getAddress(@RequestParam(value = "id",defaultValue = "-1")int id,
                      @RequestParam(value = "uid",defaultValue = "-1")int uid){
        if(uid==-1)
            return MyMessage.defaultError;
        else {
            if(id==-1)
                return JSON.toJSONString(AddressManage.getByUid(uid));
            else
                return JSON.toJSONString(AddressManage.get(id));

        }
    }

    @RequestMapping("/address/add")
    public @ResponseBody
    String addAddress(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                      @RequestParam(value = "address",defaultValue = "")String address,
                      @RequestParam(value = "phone",defaultValue = "")String phone){
        if(uid==-1)
            return MyMessage.defaultError;
        if(address.trim().length()<1)
            return MyMessage.toJson("地址为空！");
        if(phone.trim().length()<1)
            return MyMessage.toJson("电话为空");

        AddressData data=new AddressData();
        data.set(uid,address,phone);
        if(AddressManage.add(data))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }

    @RequestMapping("/address/update")
    public @ResponseBody
    String updateAddress(@RequestParam(value = "id",defaultValue = "-1")int id,
                      @RequestParam(value = "address",defaultValue = "")String address,
                      @RequestParam(value = "phone",defaultValue = "")String phone){
        if(id==-1)
            return MyMessage.defaultError;
        if(address.trim().length()<1)
            return MyMessage.toJson("地址为空！");
        if(phone.trim().length()<1)
            return MyMessage.toJson("电话为空");

        AddressData data=AddressManage.get(id);
        if(data==null)
            return MyMessage.defaultSqlNotFoundError;

        data.setAddress(address);
        data.setPhone(phone);
        if(AddressManage.update(data))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }

    @RequestMapping("/address/del")
    public @ResponseBody
    String delAddress(@RequestParam(value = "id",defaultValue = "-1")int id,
                      @RequestParam(value = "uid",defaultValue = "-1")int uid){
        if(uid==-1)
            return MyMessage.defaultError;
        else {
            if(id==-1){
                if(AddressManage.delByUid(uid))
                    return MyMessage.defaultSuccess;
            }
            else {
                if(AddressManage.del(id))
                    return MyMessage.defaultSuccess;
            }
        }
        return MyMessage.defaultSqlError;
    }
}
